<template>
	<view class="log-container">
		<view class="date-choose-box">
			<image src="@/static/index/calendar.png" mode="widthFix" class="calendar-icon"></image>
			<picker mode="date" :data-index='0' :value="starTime" @change="bindDateChange" :end="currentDate">
				<view class="select-input" v-if="starTime">{{starTime}}</view>
				<view class="select-input" v-else>请选择开始时间</view>
			</picker>
			~
			<picker mode="date" :data-index='1' :value="date" @change="bindDateChange" :start="starTime" :end="currentDate">
				<view class="select-input" v-if="endTime">{{endTime}}</view>
				<view class="select-input" v-else>请选择结束时间</view>
			</picker>
		</view>
		
		<view class="list-box" v-if="list.length>0">
			<view class="list-item" v-for="(item,index) in list" :key="index">
				<view class="time">时间：{{item.data_time}}</view>
				<view class="record">操作日志：{{item.remarks}}</view>
			</view>
		</view>
		<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
		<view class="empty-box" v-else>
			<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			const currentDate = this.getDate(0)
			return{
				deviceId:'',
				lineNum:'',
				starTime:currentDate,
				endTime:currentDate,
				currentDate: currentDate,
				list:[],
				
				page: 1,
				size: 10,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
			}
		},
		
		onLoad(ops) {
			this.deviceId=ops.deviceId
			this.lineNum=ops.lineNum
			this.getList()
		},
		onPullDownRefresh() {
			this.freshList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		
		onReachBottom() {
			if (this.more) {
				this.getList()
			}
		},
		methods:{
			// 获取时间（n：几天前的时间，0为当天）
			getDate(n) {
				const date = new Date();
				date.setDate(date.getDate() - n);
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.starTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
				this.freshList()
			},
			
			freshList(){
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.list = []
				this.getList();
			},
			
			// 分页获取日志列表
			getList(){
				this.$request.post(this.$api.switchsLlightLog, {
					store_id:uni.getStorageSync('store_id'),
					device_id:this.deviceId,
					device_num:this.lineNum,
					page:this.page++,  //当前页数
					show_page:this.size,  //当前条数
					start_time:this.starTime?this.starTime+" 00:00:00":"",
					end_time:this.endTime?this.endTime+" 23:59:59":"",
				}, {
					token: true
				}).then(res => {
					if(res.code==1){
						let {
							data = {}
						} = res;
						let newArray = data
						if (newArray&&newArray.length == 0 && this.page == 2) {
							this.showEmpty = true
							this.showLoadStatus = false
							this.more = false // 不能再次请求了
							return false;
						} else {
							this.showLoadStatus = true
							this.more = true
							this.showEmpty = false
							this.status = 'loading'
						}
						if (newArray&&newArray.length < this.size) { // 说明没有更多数据
							this.status = 'noMore'
							this.more = false
							this.showEmpty = false
						}
						this.list = this.list.concat(newArray);
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.log-container{
		padding: 0 36rpx 32rpx 36rpx;
	}
	.date-choose-box{
		width: 100%;
		padding: 32rpx 0 20rpx 0;
		background: #040336;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		gap:20rpx;
		font-size: 28rpx;
		position: sticky;
		top: 0;
		left: 0;
		.calendar-icon{
			width: 40rpx;
			height: 40rpx;
		}
	}
	
	.list-box{
		// margin-top: 20rpx;
		.list-item{
			padding: 30rpx 0;
			&:not(:last-child){
				border-bottom: 1rpx solid rgba(255,255,255,0.2);
			}
			.time{
				color: rgba(255,255,255,0.7);
				font-size: 28rpx;
			}
			.record{
				margin-top: 6rpx;
				color: rgba(255,255,255,1);
				font-size: 28rpx;
			}
		}
	}
</style>